<?php
declare(strict_types=1);

namespace App\Wxc\Service;

use App\Wxc\Concern\ManageChannelsShopApp;
use App\Wxc\Mapper\WxcProductMapper;
use App\Wxc\Model\WxcProduct;
use App\Wxc\Model\WxcProductSpec;
use App\Wxc\Request\WxcProductSaveRequest;
use App\Wxc\Service\Query\ProductQuery;
use App\Wxc\Service\Query\ShopCfgQuery;
use Deepwell\EasyQuery\Concern\QueryService;
use Hyperf\Database\Model\Collection;
use Mine\Annotation\Transaction;
use Mine\Traits\ServiceTrait;

/**
 * 商品管理服务类
 * @method WxcProduct|null queryOne(?ProductQuery $input = null)
 * @method Collection<int, WxcProduct> queryList(?ProductQuery $input = null)
 */
class WxcProductService
{
    use ServiceTrait;
    use ManageChannelsShopApp;
    use QueryService;

    public function __construct()
    {
        $this->queryModelClassName = WxcProduct::class;
        $this->mapper = container()->get(WxcProductMapper::class);
    }

    public function sync()
    {
        $shopServ = container()->get(WxcShopCfgService::class);
        $getShop = new ShopCfgQuery();
        $getShop->select('app_id');
        $shopConfList = $shopServ->queryList($getShop);

        $queueServ = container()->get(WxcQueueService::class);

        foreach ($shopConfList as $conf) {
            $queueServ->syncProduct($conf->app_id);
        }

        return ['status' => 'promise', 'message' => '同步中'];
    }

    #[Transaction]
    public function updateSpec(WxcProductSaveRequest $data): array
    {
        // product表存微信端原生字段，一般不做操作
        // product_spec表存二次开发所需字段
        $results = [];
        foreach ($data->product_id_list as $productId) {
            /** @var WxcProduct $p */
            $p = WxcProduct::find($productId);
            if (!$p) {
                $results[$productId] = false;
                continue;
            }
            // spec存在则更新，否则新增
            $spec = $p->spec;
            if (!$spec) {
                $spec = new WxcProductSpec();
                $spec->product_id = $productId;
                $spec->app_id = $p->app_id;
            }
            $spec->use_commission = $data->use_commission;
            $spec->commission_rate = $data->commission_rate;
            $results[$productId] = $spec->save();
        }

        return $results;
    }
}